<script src="{{ getAssets }}js/bivariatetable/bivariatetable.js" type="text/javascript"></script>
<script type="text/javascript">
var goods_id = '{{.goods_id}}';
var entry_id = '{{.entry_id}}';
var middle_id = '{{.middle_id}}';
var ranges = '{{.ranges}}';
var goods_snd = JSON.parse(ranges);
var bivariatetable;
$(function(){
	$.post('{{ urlfor "ErpEntryController.LensBivariate" }}',{'goods_id':goods_id, 'middle_id': middle_id}, function(res){
		if(res.code == 0) {
			$('#goods-title').html(productGroupName(res.data.goods));
            bivariatetable = setBivariatetable(goods_snd, res.data.middle);
		}
	}, 'json');
});

function setBivariatetable(goods_snd, goods_detail) {
	var cyl_min = parseFloat(goods_snd.cyl_min);
	var cyl_max = parseFloat(goods_snd.cyl_max);
	var cyl_step = parseFloat(goods_snd.cyl_step);
	var sph_min =  parseFloat(goods_snd.sph_min);
	var sph_max =  parseFloat(goods_snd.sph_max);
	var sph_step =  parseFloat(goods_snd.sph_step);
	var x  = [];
	var y = [];
	var valueInfo = [];
	if(cyl_min==cyl_max){
		var new_va =  formatnumber(cyl_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		x.push(new_va);
	}else{
		for(var i = cyl_min;i<=cyl_max;i=i+cyl_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			x.push(new_va);
		}
	}
	if(sph_min==sph_max){
		var new_va =  formatnumber(sph_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		y.push(new_va);
	}else{
		for(var i = sph_min;i<=sph_max;i=i+sph_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			y.push(new_va);
		}
	}
	
	if(sph_min<0&&sph_max<=0){
		y = y.sort(function(a,b){
			return b-a;
		});
	}
	if(cyl_min<0&&cyl_max<=0){
		x = x.sort(function(a,b){
			return b-a;
		});
	}

	for(var v in goods_detail){
		valueJson={};
		valueJson.sph = goods_detail[v].Snd.sph;
		valueJson.cyl = goods_detail[v].Snd.cyl ? goods_detail[v].Snd.cyl : goods_detail[v].Snd.add;
		valueJson.purchase_num = goods_detail[v].purchase_num;
		valueInfo.push(valueJson);
	}


	var dd = $("#bivariate-table").bivariatetable({
		xaxis: x,
		yaxis: y,
		defaultposition: {cyl: x[0], sph: y[0]},
		defaultcolor: "#aaa",
		defaultbgcolors: "#fff",
		xkey: "cyl",
		ykey: "sph",
        reloads:false,
		valuekey: "purchase_num",
		data: valueInfo,
		onselect: function(callback) {
			layer.prompt({title: '输入采购数量'}, function(number, index){
				  layer.close(index);
				  callback({purchase_num : +number});
				  mathTotalNum();
			});
		}
	});
    //$("#bivariate-table").find('.bivariate-table-row > span').attr('contenteditable', true);
	mathTotalNum();
	return dd;
}

function mathTotalNum(){
	var totamNum= 0;
	var value = $("#bivariate-table").bivariatetable().getData();
	if(value.length==0){
		$("#totoalnumber").text(0);
		return;
	}
	for(var v in value){
		totamNum = parseInt(value[v].purchase_num)+totamNum;
	}
	$("#totoalnumber").text(totamNum);
}
$('#cennelBtn').click(function(){
	parent.closeLensBivariate();
	return false;	
});

//获取表格复制数据
$('#pasteline').click(function () {
    layer.msg("试一试 Ctrl+V 有惊喜！");
});
document.addEventListener("paste", function () {
    if (event.clipboardData || event.originalEvent) {
        var clipboardData = (event.clipboardData || window.clipboardData);
        var val = clipboardData.getData('text');
        if(val == '') {
            layer.msg("没有获取到内容");return false;
        }
        var rows = val.split("\r\n");
        var valueJson = [];
        $.each(rows, function(idx, value) {
            var column = value.split("\t");
            var snd = {};
            var columns = [];
            $.each(column, function (k, v) {
                if(v == '' || v == undefined || v == 'undefined') {
                }else {
                    columns.push(v);
                }
            });
            if(columns.length > 0) {
                var sph = formatnumber(columns[0]);
                var cyl = formatnumber(columns[1]);
                var num = columns[2] ? parseInt(columns[2]) : 0;
                sph = sph>0?'+'+sph:sph;
                cyl = cyl>0?'+'+cyl:cyl;
                valueJson.push({'sph':sph, 'cyl':cyl, 'purchase_num':num});
            }
        });
        bivariatetable.setData(valueJson, false);
        event.preventDefault();
    }
});

$('#submitBtn').click(function(){
	var value = $("#bivariate-table").bivariatetable().getData();
	if(value.length==0){
		layer.alert("请先框选光度");
		return;
	}
	var value_format = [];
	var purchase_num = 0;
	$.each(value, function(idx, val){
		value_format.push({
			'cyl':val.cyl+'',
			'sph':val.sph+'',
			'purchase_num':parseInt(val.purchase_num),
		});
		purchase_num += parseInt(val.purchase_num);
	});

	
	$.post('{{ urlfor "ErpEntryController.SaveEntryData" }}', {'purchase_num': purchase_num, 'middle_id': middle_id,'goods_id':goods_id,'entry_id':entry_id, 'ranges':ranges, 'dataInfo':JSON.stringify(value_format)}, function(res){
		if(res.code == 1) {
			layer.alert(res.msg);
			return false;	
		}
		parent.closeSelectComplete();
	}, 'json');
	
	return false;
});
</script>